package com.ins1st.plus.service.system.menu.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ins1st.plus.api.system.menu.entity.SysMenu;
import com.ins1st.plus.api.system.menu.entity.Ztree;
import com.ins1st.plus.api.system.menu.service.SysMenuApi;
import com.ins1st.plus.api.system.role.entity.SysRoleMenu;
import com.ins1st.plus.api.system.role.entity.SysUserRole;
import com.ins1st.plus.api.system.role.service.SysRoleMenuApi;
import com.ins1st.plus.api.system.role.service.SysUserRoleApi;
import com.ins1st.plus.base.ServiceResult;
import com.ins1st.plus.constants.MenuConstants;
import com.ins1st.plus.service.system.menu.mapper.SysMenuMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 系统菜单表 服务实现类
 * </p>
 *
 * @author sun
 * @since 2019-09-04
 */
@Service(interfaceClass = SysMenuApi.class, timeout = 3000)
@Component
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuApi {

    @Autowired
    SysRoleMenuApi sysRoleMenuApi;
    @Autowired
    SysUserRoleApi sysUserRoleApi;
    @Autowired
    SysMenuApi sysMenuApi;

    @Override
    public List<SysMenu> queryMenus4Index(Integer id) {
        List<SysUserRole> sysUserRoleList = sysUserRoleApi.list(new QueryWrapper<SysUserRole>().eq("user_id", id));
        List<SysMenu> result = new ArrayList<>();
        for (SysUserRole sysUserRole : sysUserRoleList) {
            List<SysRoleMenu> sysRoleMenuList = this.sysRoleMenuApi.list(new QueryWrapper<SysRoleMenu>()
                    .eq("role_id", sysUserRole.getRoleId()));
            for (SysRoleMenu sysRoleMenu : sysRoleMenuList) {
                result.add(this.sysMenuApi.getById(sysRoleMenu.getMenuId()));
            }
        }
        for (int i = 0; i < result.size(); i++) {
            for (int j = 0; j < result.size(); j++) {
                if (result.get(i).getpId().equals(result.get(j).getId()) && !result.get(i).getLevel().equals("3")) {
                    if (result.get(j).getSysMenuList() == null) {
                        result.get(j).setSysMenuList(new ArrayList<>());
                    }
                    result.get(j).getSysMenuList().add(result.get(i));
                }
            }
        }
        Iterator<SysMenu> it = result.iterator();
        while (it.hasNext()) {
            SysMenu sysMenu = it.next();
            if (sysMenu.getLevel().equals("3") || sysMenu.getIsMenu().equals("2") || sysMenu.getLevel().equals("2")) {
                it.remove();
            }
        }
        return result;
    }


    @Override
    public boolean delete(Integer id) {
        this.baseMapper.deleteById(id);
        List<SysMenu> children = this.baseMapper.selectList(new QueryWrapper<SysMenu>().eq("p_id", id));
        for (SysMenu child : children) {
            this.baseMapper.deleteById(child.getId());
            this.baseMapper.delete(new QueryWrapper<SysMenu>().eq("p_id", child.getId()));
        }
        return true;
    }

    @Override
    public ServiceResult saveMenu(SysMenu sysMenu) {
        if (!StringUtils.equals(MenuConstants.LEVEL_1, sysMenu.getLevel()) &&
                !StringUtils.equals(MenuConstants.LEVEL_2, sysMenu.getLevel()) &&
                !StringUtils.equals(MenuConstants.LEVEL_3, sysMenu.getLevel())) {
            return ServiceResult.error("菜单只支持到3级");

        }
        this.baseMapper.insert(sysMenu);
        return ServiceResult.success();
    }

    @Override
    public ServiceResult queryMenus4Role(Integer roleId) {
        List<SysMenu> sysMenuList = this.baseMapper.selectList(null);
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuApi.list(new QueryWrapper<SysRoleMenu>().eq("role_id", roleId));
        List<Ztree> ztrees = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            Ztree ztree = new Ztree();
            ztree.setId(sysMenu.getId());
            ztree.setpId(sysMenu.getpId());
            ztree.setName(sysMenu.getName());
            ztree.setLevel(sysMenu.getLevel());
            if (sysMenu.getpId() == 0) {
                ztree.setParent(true);
            }
            for (SysRoleMenu sysRoleMenu : sysRoleMenuList) {
                if (sysMenu.getpId().equals(sysRoleMenu.getMenuId())) {
                    ztree.setChecked(true);
                }
            }
            ztrees.add(ztree);
        }
        return ServiceResult.success(ztrees);
    }
}
